package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    //通过月份获取订单预约信息
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //对月份做下加0处理  2020-9
        StringBuffer sb = new StringBuffer(date);
        String newDate = sb.substring(5).length() == 1 ? sb.insert(5, "0").toString() : sb.toString();

        return orderSettingMapper.getOrderSettingByMonth(newDate);
    }

    @Override
    public void add(List<OrderSetting> orderSettings) {
        //判空
        if (orderSettings != null && orderSettings.size() > 0) {
            for (OrderSetting os : orderSettings) {
                //先判断数据是否已存在
                Long count = orderSettingMapper.findCountByOrderDate(os.getOrderDate());
                if (count > 0) {
                    //数据已存在 更新
                    orderSettingMapper.editNumberByOrderDate(os);
                } else {
                    //数据不存在 添加
                    orderSettingMapper.add(os);
                }
            }
        }
    }

    @Override
    public void edit(OrderSetting orderSetting) {
        //先判断数据是否已存在
        Long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //数据已存在 更新
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        } else {
            //数据不存在 添加
            orderSettingMapper.add(orderSetting);
        }
    }

    @Override
    public void deleteByDate(String date) {
        orderSettingMapper.deleteByDate(date);
    }
}
